package Utils;


import org.apache.commons.codec.binary.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;



public class AESCipher {

    private static final String IV_STRING = "A-16-Byte-String";
    private static final String charset = "UTF-8";
    
    public static final String STAFF_HTTP_ENCRYPT_KEY = "0g5e!ncehrateKey";

    public static String aesEncryptString(String content, String key) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        byte[] contentBytes = content.getBytes(charset);
        byte[] keyBytes = key.getBytes(charset);
        byte[] encryptedBytes = aesEncryptBytes(contentBytes, keyBytes);
        return new String(new Base64().encode(encryptedBytes), charset);
    }

    public static String aesDecryptString(String content, String key) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        byte[] encryptedBytes = new Base64().decode(content);
        byte[] keyBytes = key.getBytes(charset);
        byte[] decryptedBytes = aesDecryptBytes(encryptedBytes, keyBytes);
        return new String(decryptedBytes, charset);
    }

    public static byte[] aesEncryptBytes(byte[] contentBytes, byte[] keyBytes) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        return cipherOperation(contentBytes, keyBytes, Cipher.ENCRYPT_MODE);
    }

    public static byte[] aesDecryptBytes(byte[] contentBytes, byte[] keyBytes) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        return cipherOperation(contentBytes, keyBytes, Cipher.DECRYPT_MODE);
    }

    private static byte[] cipherOperation(byte[] contentBytes, byte[] keyBytes, int mode) throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
        SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");

        byte[] initParam = IV_STRING.getBytes(charset);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(mode, secretKey, ivParameterSpec);

        return cipher.doFinal(contentBytes);
    }

    public static void main(String...args) throws Exception{
        System.out.println(aesDecryptString("/j6iUcgMO45J4oEF6zCEh49gNiyA9Jq+HGWYGIYLHQJafzXadd99mhNzoL2Luzpa9T8CnPty118JC4I7pXLUG7VN6Xs/Zh1EGsCd3eK/9Wgqn5OKvSiGlcQX/wKmcsXy+wCs2rZXhFvvlgzlvsJTYwtw+dyawebOq5keOHFW+w99KwLi8F3FjwcikxmjpL8mpg1hTP2WfeiuBUDgQ9zPI9b8PWxRhvQ2GzHcJecYyc/XvNMGs3lppcLIha3j+drVOR7HFpzE/eZtzs/XxypwH0Q8gRkjhYP/ZcfSyJCpzBMAE/7Xscq2WNV/WlUp7Ny79C5uGTNmz04o/ev40X1IVXscSROVtiR88RqurubSAWhD1YB3CehYM0giEZ31od8HBRyNV8BPfXlob6ErirNqQMYX2pp7erme94Nnqwa9NTQQco4CIhAPm5AgWoFE94Kz",
                STAFF_HTTP_ENCRYPT_KEY));
    }

}
